package com.helasasa.maxfunds.lineofcredit.cashloan.utils.log

import android.util.Log
import okhttp3.logging.HttpLoggingInterceptor

class ApiLoggingInterceptor : HttpLoggingInterceptor.Logger {
    companion object {

        fun formatJson(jsonStr: String): String {
            var level = 0
            val jsonForMatStr = StringBuffer()
            for (element in jsonStr) {
                val c = element
                if (level > 0 && '\n' == jsonForMatStr[jsonForMatStr.length - 1]) {
                    jsonForMatStr.append(
                        getLevelStr(
                            level
                        )
                    )
                }
                when (c) {
                    '{', '[' -> {
                        jsonForMatStr.append("${c}\n")
                        level++
                    }
                    ',' -> {
                        jsonForMatStr.append("${c}\n")
                    }
                    '}', ']' -> {
                        jsonForMatStr.append("\n")
                        level--
                        jsonForMatStr.append(getLevelStr(level))
                        jsonForMatStr.append(c)
                    }
                    else -> jsonForMatStr.append(c)
                }
            }
            return jsonForMatStr.toString()
        }

        fun getLevelStr(level: Int): String {
            val levelStr = StringBuffer()
            for (levelI in 0 until level) {
                levelStr.append("\t")
            }
            return levelStr.toString()
        }

        fun jsonLog(message: String) {
            if (message.length > 4000) {
                var i = 0
                while (i < message.length) {
                    if (i + 4000 < message.length) {
                        Log.i(
                            ApiLoggingInterceptor::class.java.simpleName + i,
                            formatJson(message.substring(i, i + 4000))
                        )
                    } else {
                        Log.i(
                            ApiLoggingInterceptor::class.java.simpleName + i,
                            formatJson(message.substring(i, message.length))
                        )
                    }
                    i += 4000
                }
            } else {
                Log.i(ApiLoggingInterceptor::class.java.simpleName, formatJson(message))
            }
        }
    }

    override fun log(message: String) {
        if (message.length > 4000) {
            var i = 0
            while (i < message.length) {
                if (i + 4000 < message.length) {
                    Log.i(
                        ApiLoggingInterceptor::class.java.simpleName + i,
                        formatJson(message.substring(i, i + 4000))
                    )
                } else {
                    Log.i(
                        ApiLoggingInterceptor::class.java.simpleName + i,
                        formatJson(message.substring(i, message.length))
                    )
                }
                i += 4000
            }
        } else {
            Log.i(ApiLoggingInterceptor::class.java.simpleName, formatJson(message))
        }
    }
}